package com.amkai.aes.webservice.mobile.objecthandler;

import com.amkai.aes.generatedbeans.passport.Passport;
import com.amkai.aes.server.hibernate.managment.PassportManager;
import com.amkai.aes.webservice.mobile.MobileJobObjectHandler;
import com.amkai.mobile.bean.passport.MobilePassport;
import com.amkai.mobile.bean.registration.MobilePerson;
import com.amkai.mobile.common.job.login.MobileLoginJob;
import com.amkai.mobile.common.job.login.MobileLoginJobResult;

public class MobileLoginJobHandler implements MobileJobObjectHandler<MobileLoginJob, MobileLoginJobResult> {

	@Override
	public MobileLoginJobResult process(MobileLoginJob job) throws Exception {
		PassportManager pm = new PassportManager();
		Passport p = pm.getPassport(job.userName, job.password);
		MobileLoginJobResult lresult = new MobileLoginJobResult();
		lresult.setSuccess(false);

		try {
			if (pm.canLogin(p)) {

				lresult.setSuccess(true);
				MobilePassport passport = new MobilePassport();

				passport.id = p.getId();

				MobilePerson person = new MobilePerson();
				person.id = (1L);
				person.firstName = p.getPerson().getFirstName();
				person.lastName = p.getPerson().getLastName();
				passport.person = person;
				passport.userName = p.getName();
				lresult.setPassport(passport);
			}
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e);
		}

		return lresult;
	}

}
